package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"shuqi-novel/models/mysql"

	"shuqi-novel/userrpc/internal/svc"
	"shuqi-novel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookPerfectLogic {
	return &BookPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说更新
func (l *BookPerfectLogic) BookPerfect(in *userrpc.BookPerfectRequest) (*userrpc.BookPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var findBook mysql.Books
	err := findBook.FindBook(mysql.DB, in.BookId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	book := &mysql.Books{
		Model:    gorm.Model{ID: uint(in.BookId)},
		Title:    in.Title,
		Comment:  in.Comment,
		BookType: in.BookType,
	}
	err = book.PerfectBook(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &userrpc.BookPerfectResponse{
		BookId: int64(book.ID),
	}, nil
}
